<?php
/**
 * Created by IntelliJ IDEA.
 * User: HXL
 * Date: 2018/7/16
 * Time: 10:17
 */
namespace app\orders\controller;

use app\common\ErrorCodes;
use think\Lang;
class State extends Base{

    public function index(){
//        $this->authVerify();
        $field = 'sid,state';
        $where = '';
        $order = '';
        $datas = $this->states()->page($field, $where, $order);
        $this->page($datas);
        $this->assign('states', $datas);
        return $this->show($datas);
//        return $this->fetch("state/index");
    }

    /**
     * 添加action
     */
    public function save(){
//        $this->authVerify();
        $datas = input('post.');
        $validate = $this->validate($datas, 'States');
        //var_dump($validate);exit();
        if(true !== $validate){
            return $this->show(array(), ErrorCodes::$FAIL, " $validate ");
//            return $this->error(" $validate ");
        }
        $result = $this->states()->add($datas, '');
        if(!$result){
            return $this->show(array(), ErrorCodes::$FAIL, Lang::get('add fail'));
//            return $this->error(Lang::get('add fail'));
        }
        return $this->show($datas, ErrorCodes::$SUCCESS, Lang::get('add success'));
//        return $this->success(Lang::get('add success'), 'State/index');
    }

    /**
     * json数据
     */
    public function one(){
        $sid = input('param.sid');
        $field = 'sid,state';
        $where = array('sid'=>$sid);
        $data = $this->states()->select($field, $where);
        return $this->show($data);
//        echo json_encode($data);
    }

    /**
     * 更新action
     */
    public function esave(){
//        $this->authVerify();
        $sid = input('param.sid');
        $where = array('sid'=>$sid);
        $find = $this->states()->findById($where);
        if(!$find){
            return $this->show(array(), ErrorCodes::$FAIL, Lang::get('unfind state'));
//            return $this->error(Lang::get('unfind state'));
        }
        $states = input('post.');
        $validate = $this->validate($states, 'States');
        //var_dump($validate);exit();
        if(true !== $validate){
            return $this->show(array(), ErrorCodes::$FAIL, " $validate ");
//            return $this->error(" $validate ");
        }
        $result = $this->states()->update($states, $where);
        if($result < 1){
            return $this->show(array(), ErrorCodes::$FAIL, Lang::get('edit fail'));
//            return $this->error(Lang::get('edit fail'));
        }
        return $this->show($states, ErrorCodes::$SUCCESS, Lang::get('edit success'));
//        return $this->success(Lang::get('edit success'), 'State/index');
    }

    /**
     * 删除action
     */
    public function del(){
//        $this->authVerify();
        $sid = input('param.sid');
        $where = array('sid'=>$sid);
        $find = $this->states()->findById($where);
        if(!$find){
            return $this->show(array(), ErrorCodes::$FAIL, Lang::get('unfind state'));
//            return $this->error(Lang::get('unfind state'));
        }
        $result = $this->states()->del($where);
        if($result < 1){
            return $this->show(array(), ErrorCodes::$FAIL, Lang::get('del fail'));
//            return $this->error(Lang::get('del fail'));
        }
        return $this->show($find, ErrorCodes::$SUCCESS, Lang::get('del success'));
//        return $this->success(Lang::get('del success'), 'State/index');
    }

    /**
     * 简单的模糊搜索
     * @return mixed
     */
    public function search(){
        $search = input('param.search');
        $data = $this->states()->search($search);
        $this->page($data);
        $this->assign('states', $data);
        return $this->show($data);
//        return $this->fetch('state/index');
    }

}